//import scala.actors.Actor
//import scala.actors.Actor._
/*
import akka.actor.Actor

case object Ping
case object Pong
case object Stop

object Main extends Application 
{
  println("Actors!!")
  val pong = new Pong
  val ping = new Ping(100000, pong)
  ping.start
  pong.start
}

class Pong extends Actor 
{
  def act() 
  {
    var pongCount = 0
    while (true) 
    {
      receive 
      {
        case Ping =>
          if (pongCount % 1000 == 0)
            Console.println("Pong: ping "+pongCount)
          sender ! Pong
          pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong: stop")
          exit()
      }
    }
  }
}

class Ping(count: Int, pong: Actor) extends Actor {
  def act() {
    var pingsLeft = count - 1
    pong ! Ping
    while (true) {
      receive {
        case Pong =>
          if (pingsLeft % 1000 == 0)
            Console.println("Ping: pong")
          if (pingsLeft > 0) {
            pong ! Ping
            pingsLeft -= 1
          } else {
            Console.println("Ping: stop")
            pong ! Stop
            exit()
          }
      }
    }
  }
}
*/

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import akka.pattern.ask
import scala.concurrent._
import akka.util.Timeout
import scala.concurrent.duration._

case class Count(CountTo : Int);
case class Result(r : Int);
case class Stop;
case class Done;

class Counter extends Actor {
  def receive = {
    case Count(countTo) => 
      {
        var c = 0;
        var count = 0;
	    for (c <- 1 to countTo)
	    {
	      for (c <- 1 to 10){}
	      count = count + 1;
	  	}
	    sender ! Result(count);
      }
    case Stop => 
      {
        sender ! Done;
        //context.stop(self);
        //context.system.shutdown();
      }
  }
}

object Main extends App {
  
  println("Starting count");
  
  val system = ActorSystem("HelloSystem")
  
  var count = 0.0;
  var c = 0.0;
  val startTime = System.currentTimeMillis();
  
  //var z = new Array[Future[Any]](101)
  
  val counter1 = system.actorOf(Props[Counter], name = "IamActor1")	  
  val counter2 = system.actorOf(Props[Counter], name = "IamActor2")	  
  implicit val timeout = Timeout(5.minutes)
  implicit val ec = system.dispatcher
  
  for (c <- 1 to 50)
  {
	  val f = counter1 ? Count(100000000)
	  f.onSuccess 
	  {
	  	case Result(r) => 
	  	  {
	  	    count = count + r
	  	    println(s"actor1 is done. count returned is $r. total count so far is $count")
	  	  }
	  }
	  
	  val f2 = counter2 ? Count(100000000)
	  f2.onSuccess 
	  {
	  	case Result(r) => 
	  	  {
	  	    count = count + r
	  	    println(s"actor2 is done. count returned is $r. total count so far is $count")
	  	  }
	  }
  }
  
  val ff = counter1 ? Stop
  Await.result(ff, Duration.Inf)
  
  val ff2 = counter2 ? Stop
  Await.result(ff2, Duration.Inf)
  
  //for( a <- 1 to 100){
  //  Await.result(z(a), 20.second)
  //}
  
  //system.awaitTermination();
  system.shutdown();
  
  val endTime   = System.currentTimeMillis();
  val totalTime = (endTime - startTime) / 1000;
  println("Time: " + totalTime + " Seconds");
  
  println("Done. Count is " + count);
  
  
}